//
//  Status.swift
//  xinlangweibo
//
//  Created by yangshuo on 15/12/3.
//  Copyright © 2015年 yangshuo. All rights reserved.
//

import UIKit

class Status: NSObject {
    //微博ID
    var id: Int64 = 0
    //微博信息内容
    var text: String?
    //微博创建时间
    var created_at: String?
    //微博来源
    var source: String?
    //配租数组(字典数组)
    var pic_urls: [[String: String]]?
    //用户
    var user: User?
    //转发微博
    var retweeted_status: Status?
    
    init(dict: [String: AnyObject]) {
        super.init()
        //如果使用KVC时,value是一个字典,会直接给属性(user)转成字典,而user中只有模型属性希望被调用,多余字典并不像被调用出来
        setValuesForKeysWithDictionary(dict)
    }
    //模型嵌套模型时,使用此方法单独处理,可以处理复杂的嵌套
    //setValuesForKeysWithDictionary默认会调用此方法
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            //在这里用户自己手动字典转模型user = User(dict: value)

            if let dict = value as? [String: AnyObject]{
            user = User(dict: dict)
                
            }
            return
    }
        //因为retweeted_status本质是一个大字典,所以需要判断key是否为retweeted_status
        if key == "retweeted_status"{
            //如果值不为空,将值转成字典
            
            if let dict = value as? [String: AnyObject]{
            //字典转模型
                retweeted_status = Status(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String{
        let keys = ["id", "text", "source", "created_at", "pic_urls"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
